package com.twu.biblioteca;
import java.io.*;

public class BibliotecaApp {
    private int ch;
    String[] BookNames=new String[20];
    public static void main(String[] args) {
        System.out.println("Welcome to Biblioteca!!");
        BibliotecaApp b = new BibliotecaApp();
        b.menu();
        System.out.println("Thank you for using Biblioteca! Visit again...");
    }

    public BibliotecaApp()
    {
        BookNames[0]="Les Miserables";
        BookNames[1]="Game of Thrones";
        BookNames[2]="The Hobbit";
        BookNames[3]="The Great Gatsby";
        BookNames[4]="Gone Girl";
        BookNames[5]="Little Women";
        BookNames[6]="The Origin of Species";
        BookNames[7]="To Kill a Mockingbird";
        BookNames[8]="Pride and Prejudice";
        BookNames[9]="The Giving Tree";
        BookNames[10]="The Kite Runner";
        BookNames[11]="Brave New World";
        BookNames[12]="The Fountainhead";
        BookNames[13]="Memoirs of a Geisha";
        BookNames[14]="The Secret Garden";
        BookNames[15]="The Alchemist";
        BookNames[16]="Catch-22";
        BookNames[17]="A Thousand Splendid Suns";
        BookNames[18]="The Count of Monte Cristo";
        BookNames[19]="Don Quixote";
    }

    //This method displays the menu of the program and calls appropriate method depending on choice of user
    public void menu()
    {
        int ch=0;
        try{
             do{
                  System.out.println("Please enter your choice:");
                  System.out.println("1. View list of books\n2. Reserve a book\n3. Check membership details\n4. Exit\n");
                  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                  ch=Integer.parseInt(br.readLine());
                  switch(ch)
                  {
                    case 1:    viewList();
                               break;

                    case 2:    reserveBook();
                               break;

                    case 3:    checkMemDetails();
                               break;

                    case 4:    break;

                    default: System.out.println("\nSelect a valid option.\n");

                  }
                }while(ch!=4);

            }
        catch(IOException ie)
        {
          ie.printStackTrace();
        }
    }

    //This method displays list of books available
    public void viewList()
    {
       for(int i=0;i< 20; i++)
       {
           System.out.println(BookNames[i]);
       }
       System.out.println("\n");
    }

    //This method reserves book for a user
    public void reserveBook()
    {
        try{
             System.out.println("Enter the name of the book to be reserved");
             BufferedReader bkName=new BufferedReader(new InputStreamReader(System.in));
             String Name=convertStreamToString(bkName);
             if(bookPresent(Name)==1)
             {
               System.out.println("Thank You! Enjoy the book.");
             }
             else
             {
               System.out.println("Sorry we don't have that book yet.");
             }
           }
           catch(Exception e)
           {
              e.printStackTrace();
           }
    }

   //This method directs user to librarian to check his/her membership details
    public void checkMemDetails()
    {
        System.out.println("Please talk to a Librarian. Thank you.\n\n");
    }

    //This method checks if the book requested is available or not
    public int bookPresent(String bkName)
    {
       int i=0;
       for(i=0;i<20;i++)
       {
           if(bkName.equals(BookNames[i]))
               return(1);
       }
       return(0);
    }

    //This method converts BufferedReader to String
    public String convertStreamToString(BufferedReader is)
    {
       java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\n");
       return s.hasNext() ? s.next() : "";
    }
}





